package com.sale.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sale.common.core.utils.StringUtils;
import com.sale.common.mybatis.core.page.PageQuery;
import com.sale.common.mybatis.core.page.TableDataInfo;
import com.sale.common.satoken.utils.LoginHelper;
import com.sale.system.domain.PullDown;
import com.sale.system.domain.SysCategory;
import com.sale.system.domain.dto.SysCategoryDto;
import com.sale.system.domain.vo.SysCategoryVo;
import com.sale.system.mapper.SysCategoryMapper;
import com.sale.system.service.ISysCategoryService;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.Date;
import java.util.List;


/**
 * @Description 类别管理实现类
 * @Date 23-12-05
 * @Author hl
 **/
@RequiredArgsConstructor
@Service
public class SysCategoryServiceImpl implements ISysCategoryService {

    private final SysCategoryMapper sysCategoryMapper;

    /**
     * 类别管理查询
     *
     * @param sysCategoryVo,pageQuery
     * @return 类别管理
     */
    @Override
    public TableDataInfo<SysCategoryDto> selectCategoryList(SysCategoryVo sysCategoryVo, PageQuery pageQuery) {
        Page<SysCategoryDto> page = sysCategoryMapper.selectCategoryList(pageQuery.build(), this.buildQueryWrapper(sysCategoryVo));
        return TableDataInfo.build(page);
    }

    /**
     * 类别管理新增
     *
     * @param sysCategoryVo
     * @return 类别管理
     */
    @Override
    public int insertCategory(SysCategoryVo sysCategoryVo) {
        SysCategory sys = new SysCategory();
        BeanUtils.copyProperties(sysCategoryVo, sys);
        sys.setCreateBy(LoginHelper.getUsername());
        sys.setCreateTime(new Date());
        return sysCategoryMapper.insert(sys);
    }

    /**
     * 类别管理查看单个
     *
     * @param categoryId
     * @return 类别管理
     */
    @Override
    public SysCategoryDto selectCategoryById(long categoryId) {
        SysCategory sys = sysCategoryMapper.selectById(categoryId);
        SysCategoryDto dto = new SysCategoryDto();
        BeanUtils.copyProperties(sys, dto);
        return dto;
    }

    /**
     * 类别管理批量删除
     *
     * @param categoryId
     * @return 类别管理
     */
    @Override
    public int deleteCategoryById(String categoryId) {
        return sysCategoryMapper.deleteBatchIds(Arrays.asList(categoryId.split(",")));
    }

    /**
     * 类别管理修改
     *
     * @param sysCategoryVo
     * @return 类别管理
     */
    @Override
    public int updateCategoryEdit(SysCategoryVo sysCategoryVo) {
        SysCategory sys = new SysCategory();
        BeanUtils.copyProperties(sysCategoryVo, sys);
        return sysCategoryMapper.updateById(sys);
    }

    /**
     * 下拉
     */
    @Override
    public List<PullDown> selectCategorySelList(String categoryType) {
        return sysCategoryMapper.selectCategorySelList(categoryType);
    }

    private Wrapper<SysCategoryVo> buildQueryWrapper(SysCategoryVo sysCategoryVo) {
        QueryWrapper<SysCategoryVo> wrapper = Wrappers.query();
        wrapper
            .like(StringUtils.isNotBlank(sysCategoryVo.getCategoryName()), "category_name", sysCategoryVo.getCategoryName())
            .eq(StringUtils.isNotBlank(sysCategoryVo.getCategoryType()), "category_type", sysCategoryVo.getCategoryType());
        return wrapper;
    }
}
